﻿using System.Configuration;
using System.Data;

using contSys.database.DAL;
using contSys.Security;
using System.Diagnostics;
using System;

namespace contSys.BL.Simulator
{
    public class Phases
    {
        private static string _connString = ConfigurationManager.ConnectionStrings["Simulator"].ConnectionString;
        private static string tableObjName = "Phases";

        public static string LastErrorMsg = string.Empty;

        public static DataSet GetPhasesByUnit(string usr, string pwd, object unitID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM Phases WHERE UnitID = {0}", unitID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }

        private static bool checkPhaseValidity(object phaseID, object phOrder)
        {
            int PhID;
            int phOrd;

            LastErrorMsg = string.Empty;

            if (!int.TryParse(phaseID.ToString(), out PhID) || PhID <= 0)
            {
                LastErrorMsg = "PhaseID must be a number larger than 0";
                return false;
            }
            if (!int.TryParse(phOrder.ToString(), out phOrd))
            {
                LastErrorMsg = "Phase order must be a number";
                return false;
            }

            return true;
        }

        public static bool ModifyPhase(string usr, string pwd, object unitID, object phaseID, object phOrder, object descr)
        {
            if (UserManager.ModifyAllowed(tableObjName, usr, pwd))
            {
                if (checkPhaseValidity(phaseID, phOrder))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("UPDATE Phases SET Description='{0}', phOrder={1} WHERE (UnitID={2} AND PhaseID={3})", descr.ToString(), phOrder.ToString(), unitID.ToString(), phaseID.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString, sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        public static bool AddPhase(string usr, string pwd, object unitID, object phaseID, object phOrder, object descr)
        {
            if (UserManager.AddAllowed(tableObjName, usr, pwd))
            {
                if (checkPhaseValidity(phaseID, phOrder))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("INSERT INTO Phases (UnitID, PhaseID, phOrder, Description) VALUES ({0},{1},{2},'{3}')", unitID.ToString(), phaseID.ToString(), phOrder.ToString(), descr.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString ,sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        public static bool DeletePhase(string usr, string pwd, object unitID, object phaseID)
        {
            if (UserManager.DeleteAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                return SQLiteDB.TryExecuteScalar(_connString, string.Format("DELETE FROM Phases WHERE (UnitID={0} AND PhaseID={1})", unitID.ToString(), phaseID.ToString()));
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }

        private static void Writelog(string msg)
        {
            const string cs = "contSys.BL.Simulator";

            EventLog elog = new EventLog();
            if (!EventLog.SourceExists(cs))
                EventLog.CreateEventSource(cs, cs);

            elog.Source = cs;
            elog.EnableRaisingEvents = true;
            elog.WriteEntry(msg);
        }
    }
}
